<?php
namespace Admin\Controller;
use Think\Controller;
class NewsController extends CommonController {


	//新闻源列表
    public function sourceList(){

    	$newsModel=D('News');
    	$keyword=I('get.keyword');
    	if($keyword){

    	 	$where['news_name']=array("like","%".$keyword."%");
    	 }
        $order="news_sort desc,id desc";
    	$comList=$newsModel->getNewsLIst($where,$order);
    	$this->assign("keyword",$keyword);
        $this->assign("comList",$comList["comList"]);
        $this->assign("page",$comList["page"]);	


        $this->display();
    }
    //新闻源新增
    public function sourceAdd(){
        
        $this->display();
    }
    //新闻源修改
    public function sourceChange(){

    	if(IS_POST){

    		$map=I('post.');
    		$data=array(
    			'news_name'=>$map['news_name'],
    			'news_url' =>$map['news_url'],
    			'news_price'=>$map['news_price'],
    			'news_sort'=>$map['news_sort'],
    			'news_note'=>$map['news_note'],
    			'add_time'=>date("Y-m-d H:i:s")
    			);
    		if(!$data['news_name'] || !$data['news_price']){
    			$this->error("请补全关键信息","");
    		}

    		if($map['id']){

    			$data['id']=$map['id'];
    			$res=M('news')->save($data);

    		}else{

    			$res=M('news')->add($data);
    		}

    		if($res){
    			$this->success("添加成功","");
    		}else{
    			$this->error("添加失败","");
    		}


    	}else{

    		$id=I('get.id');

    		if(!$id){

    			$this->error("未获取到id信息","");
    		}

    		$news_info=M('news')->where("id=".$id)->find();

	    	$this->assign("news_info",$news_info);

	        $this->display();

    	}
    }


    //云新闻订单
    public function orderList(){
        $orderModel=M('news_order n');

        $keyword=I('get.');

        if($keyword['username']){

            $where['u.username']=array("like","%".$keyword['username']."%");
         }
         if($keyword['title']){
            $where['n.news_title']=array("like","%".$keyword['title']."%");
         }
         if($keyword['apply']){
            if($keyword['apply']=='1'){
                $where['n.is_apply']='0';
            }else if($keyword['apply']=='3'){
                $where['n.is_apply']='2';
            }else if($keyword['apply']=='4'){
                $where['n.is_ok']='0';
                $where['n.is_apply']='1';
            }else if($keyword['apply']=='5'){
                $where['n.is_ok']='1';
                $where['n.is_apply']='1';
            }
         }
         $where['n.news_target']="0";
         $join="left join cloud_user u on u.id=n.user_id";
         $field="n.*,u.username";
         $comList=getPages($orderModel,$where,"",10,$join,$field);
        
       
        $this->assign("keyword",$keyword);
        $this->assign("comList",$comList["comList"]);
        $this->assign("page",$comList["page"]); 


        $this->display();
    }

    public function orderDetail(){

        $id=I('get.id');
        $orderInfo=M('news_order')->where('id='.$id)->find();
        $orderInfo['news_content']=htmlspecialchars_decode($orderInfo['news_content']);

        if($orderInfo['type']=='1'){

           $packageInfo=M('news_package')->where("id=".$orderInfo['news_ids'])->find();
            $orderInfo['news_ids']=$packageInfo['package_content'];
            $orderInfo['package_name']=$packageInfo['package_name'];

        }
        $orderInfo['news_feedback']=json_decode($orderInfo['news_feedback'],true);

        $newsList=M('news')->where(array('id'=>array('in',$orderInfo['news_ids'])))->select();
        foreach ($newsList as $key => $value) {
           $newsList[$key]['news_feedback']=$orderInfo['news_feedback'][$value['id']];
        }
        
        $this->assign("newsList",$newsList);
        $this->assign("orderInfo",$orderInfo);

        $this->display();
    }

    public function feedback(){
        $map=I('post.');
        $arr=array_combine($map['ids'],$map['case_urls']);
        $news_feedback=json_encode($arr);

        $res=M('news_order')->where('id='.$map['id'])->setField('news_feedback',$news_feedback);
        if($res){
            $this->success("操作成功","");
        }else{
            $this->error("操作失败","");
        }

    }

    public function ajaxState(){
        $map=I('post.');

        if($map['state']=='1'){
            $data['is_apply']=1;
        }else if($map['state']==2){
            $data['is_apply']=2;
            $data['reason']=$map['reason'];
        }else if($map['state']==3){
            $data['is_apply']=1;
            $data['is_ok']=1;
        }else{
            exit("0");
        }

        $data['id']=$map['id'];
      
        $res=M('news_order')->save($data);

        if($res){
            exit("1");
        }else{
            exit("0");
        }

    }
    //t套餐列表
    public function newsList(){
        $keyword=I('get.keyword');
        if($keyword){
            $where['package_name']=array("like","%".$keyword."%");
         }

        $comList=getPages(M('news_package'),$where);

        $this->assign("comList",$comList["comList"]);
        $this->assign("page",$comList["page"]); 
        $this->display();
    }

    //t套餐修改
    public function newsChange(){
        $id=I('get.id');
        $newsModel=D('news');

        $packageInfo=M('news_package')->where('id='.$id)->find();
        $packageInfo['news_ids']=explode(",", $packageInfo['package_content']);

        $newsInfo=M('news')->where(array('id'=>array("in",$packageInfo['package_content'])))->order("FIELD(id,{$packageInfo['package_content']}) desc")->select();

        foreach ($newsInfo as $key => $value) {
            $price[]=$value['news_price'];
        }
        $totalPrice=array_sum($price);

        $p = I('page') ? I('page') : 0;
        $keyword=I('keyword');
        if($keyword){

            $where['news_name']=array("like","%".$keyword."%");
         }
    
        $pagesize=36;
        $start =$p!=0?($p-1)*$pagesize:$p*$pagesize;
        $order="FIELD(id,".$packageInfo['package_content'].") desc,news_sort desc,id desc";

        $newsList=$newsModel->getNewsLIst($where,$order,$p,$pagesize);
       

        if($p>1 || $keyword )
        {   

               
               echo json_encode($newsList);
               
               exit;
        }else{

            if(!$id){
                $this->error("未获取到id信息","");
            }
            $sum =  $newsModel->count();
            $this->assign("totalPages",ceil($sum/$pagesize));
            $this->assign("totalPrice",$totalPrice);
            $this->assign("newsInfo",$newsInfo);
            $this->assign("packageInfo",$packageInfo);
            $this->assign("comList",$newsList);
            $this->assign("keyword",$keyword);
            $this->display();
        } 

       
    }


    public function packageForm(){

        $map=I('post.');
       
        $data=array(
            'package_name'=>$map['package_name'],
            'package_content'=>implode(",", $map['news_ids']),
            'package_type'=>$map['package_type'],
            'price'=>$map['price']
            );
        if(!$data['package_name'] || !$data['package_content'] || !$data['price'] ){
            $this->error("请补全关键信息","");
        }
        if($map['id']){
            $data['id']=$map['id'];
            $res=M('news_package')->save($data);
        }else{
            $data['add_time']=date("Y-m-d H:i:s");
            $res=M('news_package')->add($data);
        }
        if($res){
            $this->success("添加成功","");
        }else{
            $this->error("添加失败","");
        }


    }

    public function newsAdd(){

        $newsModel=D('news');
        $p = I('page') ? I('page') : 0;
        $keyword=I('keyword');
        if($keyword){

            $where['news_name']=array("like","%".$keyword."%");
         }
       
        $pagesize=36;
        $start =$p!=0?($p-1)*$pagesize:$p*$pagesize;
        $order="news_sort desc,id desc";

        $newsList=$newsModel->getNewsLIst($where,$order,$p,$pagesize);



         if($p>1 || $keyword )
        {   

               
               echo json_encode($newsList);
               
               exit;
        }else{
         
            $sum =  $newsModel->count();
            $this->assign("totalPages",ceil($sum/$pagesize));
            $this->assign("comList",$newsList);
            $this->assign("keyword",$keyword);
            $this->display();
        } 
    }
}